package com.yc.jpyy.vehicle.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.utils.Tools;
import com.yc.jpyy.vehicle.dao.VehicleDao;
import com.yc.jpyy.vehicle.service.VehicleService;



@Controller
@RequestMapping(value = "/test")
public class TestController extends BaseController{

	@Autowired
	private VehicleService vehicleService;

	@Autowired
	private VehicleDao vehicleDao;
	
	/**
	 * 切换数据源测试
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/testChangeDataSource", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String testChangeDataSource(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			HandlerDataSource.setDataSource("A1");
			String parameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(parameter);
			resultMap = vehicleService.selects(paramsMap);
			HandlerDataSource.setDataSource("A101");
			resultMap = vehicleService.selects(paramsMap);
		} catch (Exception e) {
			System.out.println("testTransaction" + e.toString());
		} finally {
			return JSON.toJSONString(resultMap);
		}
	}
	

	@SuppressWarnings("finally")
	@RequestMapping(value = "/testTransaction", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String testTransaction(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			HandlerDataSource.setDataSource("A1");
			String parameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(parameter);
			resultMap = vehicleService.update(paramsMap);
		} catch (Exception e) {
			System.out.println("testTransaction" + e.toString());
		} finally {
			return JSON.toJSONString(resultMap);
		}
	}

}
